<?php

namespace App\Http\Controllers\Admin\File;

use App\Http\Models\Admin\File\File;
use App\Http\Responses\ResponseDto;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Input;
use App\Services\Admin\FileManageService;
use Intervention\Image\Facades\Image;
use Psy\Util\Json;

class FileUploadController extends Controller
{



    public function __construct(FileManageService $service)
    {
        $this->FileService = $service;
    }


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }
    public function uploadfile(Request $request)
    {
        $file = Input::file('file');

        if($file){
            $data = json_decode(stripslashes($_POST['avatar_data']));
            $isphoto =  $request->input("isphoto");
//            var_dump($isphoto);
            $filepath = "uploads/".$request->input("savefolder")."/".date('Ymd')."/";//这里是用户最终裁切好的头像存放目录，当然你可以按年月日目录结构来存放
            if(!file_exists($filepath)){
                mkdir($filepath,0777,true);
            }
            //文件名, 属性
            $entension = $file->getMimeType();
            $tmp_file_name =Carbon::now()->format('His')."_".$file->getClientOriginalName();

            $img = Image::make($file)->crop(intval($data->width), intval($data->height), intval($data->x), intval($data->y));

            if($isphoto == 1){
                $photo_size = Config::get("common.photo_size");
                $p_tag = $photo_size[0]."x".$photo_size[1]."_";
                $ba_file_path =$filepath.$p_tag.$tmp_file_name;
                $img->resize($photo_size[0], $photo_size[1])->save($ba_file_path);
            }else{

                $ba_file_path = $filepath.$tmp_file_name;
                $img->resize(intval($data->width), intval($data->height))->save($ba_file_path);

                $file_size =round($file->getSize() / 1024);
                $file_name = $file->getClientOriginalName();
                $fileextension = $file->getClientOriginalExtension();
                $file = new File();
                $file->file_name =$file_name;
                $file->store_name =$tmp_file_name;
                $file->path =$ba_file_path;
                $file->size =$file_size;
                $file->type =strtolower($entension);
                $file->extension =strtolower($fileextension);
                $this->FileService->save($file);

            }






            $resule = array(
                "file_path"=>$ba_file_path,
                "full_file_path"=>url($ba_file_path)
            );
            return ResponseDto::createResObj($resule,ResponseDto::$SUCCESS);

        }else{
            return ResponseDto::createResObj(trans("admin/message.image_upload_fail"),ResponseDto::$FAIL);

        }

    }

    public function uploadSummernotefile(Request $request)
    {
        $file = Input::file('file');

        if($file){

            $filepath = "uploads/images/".date('Ymd')."/";//这里是用户最终裁切好的头像存放目录，当然你可以按年月日目录结构来存放

//            var_dump($filepath);
            if(!file_exists($filepath)){
                mkdir($filepath);
            }
            //文件名, 属性
            $entension = $file->getMimeType();
            $file_name = $file->getClientOriginalName();
            $fileextension = $file->getClientOriginalExtension();
            $file_size =round($file->getSize() / 1024);

            $tmp_file_name =Carbon::now()->format('His')."_".$file_name;
            $ba_file_path = $filepath.$tmp_file_name;
            //
            $img = Image::make($file);
            $img->save($ba_file_path);
           // move_uploaded_file($location, $destination);




            $file = new File();
            $file->file_name =$file_name;
            $file->store_name =$tmp_file_name;
            $file->path =$ba_file_path;
            $file->size =$file_size;
            $file->type =strtolower($entension);
            $file->extension =strtolower($fileextension);
            $this->FileService->save($file);


            $resule = array(
                "code"=>200,
                "url"=>url($ba_file_path),

            );
            return Json::encode($resule);


        }else{
            $resule = array(
                "code"=>500,
            );
            return Json::encode($resule);

        }

    }
    public function uploadJQfile(Request $request)
    {
        $file = Input::file('files');


        if($file){
            $file = $file[0];

            $filepath = "uploads/files/".date('Ymd')."/";//这里是用户最终裁切好的头像存放目录，当然你可以按年月日目录结构来存放

//            var_dump($filepath);
            if(!file_exists($filepath)){
                mkdir($filepath);
            }
            //文件名, 属性
            $entension = $file->getMimeType();
            $file_name = $file->getClientOriginalName();
            $fileextension = $file->getClientOriginalExtension();
            $file_size =round($file->getSize() / 1024);

            $tmp_file_name =Carbon::now()->format('His')."_".$file_name;
            $ba_file_path = $filepath.$tmp_file_name;
            $path = $file->move($filepath,$tmp_file_name);

            $file = new File();
            $file->file_name =$file_name;
            $file->store_name =$tmp_file_name;
            $file->path =$ba_file_path;
            $file->size =$file_size;
            $file->type =strtolower($entension);
            $file->extension =strtolower($fileextension);
            $this->FileService->save($file);


            $resule = array(
                "files"=>array(
                    array(
                        "url"=>url($ba_file_path),
                        "name"=>$file_name,
                        )
                )

            );
            return Json::encode($resule);


        }else{
            $resule = array(
                "code"=>500,
            );
            return Json::encode($resule);

        }

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
